package com.example.springboot.controller;

import com.example.springboot.mapper.DepartmentMapper;
import com.example.springboot.mapper.EmployeeMapper;
import com.example.springboot.pojo.Department;
import com.example.springboot.pojo.Employee;
import com.fasterxml.jackson.databind.annotation.JsonAppend;
import org.apache.catalina.webresources.StandardRoot;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Collection;

@Controller
public class EmployeeController {

    @Autowired
    EmployeeMapper employeeMapper;

    @Autowired
    DepartmentMapper departmentMapper;

    @RequestMapping("/emp/go")
    public String goEmployee(Model model){
        Collection<Employee> employees = employeeMapper.getAll();
        model.addAttribute("emps",employees);
        return "emp/EmployeeShow";
    }

    @RequestMapping("/emp/add")
    public String empAdd1(Model model){
        Collection<Department> departments = departmentMapper.getDepartment();
        model.addAttribute("departments",departments);
        return "emp/empadd";
    }

    @RequestMapping("/empadd")
    public String empAdd2(Employee employee){
        System.out.println("----------------------");
        System.out.println(employee);
        employeeMapper.save(employee);
        return "redirect:/emp/go";
    }

    @RequestMapping("/emp/update{id}")
    public String toUpdateEmp(@PathVariable("id") Integer id,Model model){
        Employee employee = employeeMapper.getEmployeeById(id);
        model.addAttribute("emp",employee);
        return "emp/empUpdate";
    }

    @RequestMapping("/updateMain")
    public String empUpdate2(Employee employee){
        employeeMapper.save(employee);
        return "redirect:/emp/go";
    }

    @RequestMapping("/emp/delete{people}")
    public String empDelete(@PathVariable("people") Employee employee){
        employeeMapper.DeleteEmp(employee);
        return "redirect:/emp/go";
    }


}
